# coding: utf8
"""
py_magic.py
python 魔法函数
"""


class Cls_for_call(object):
	"""
	__call__  让python实例对象像方法一样可传参调用
	"""
	def __call__(self, name):
		"""
		实例对象直接调用的方法
		"""
		print ('name is : %s' % name)

# instance = Cls_for_call()
# instance('zsl')

instances = {}
class Cls_for_new(object):
	"""
	__new__ 必须有返回值, 返回的是父类的new, 
	如果返回的对象不正确, __init__方法可能无法执行
	"""
	_instance = None
	def __init__(self):
		print ('my name is __init__: %s' % self)

	def __new__(cls):
		if not cls._instance:
			cls._instance = super(Cls_for_new, cls).__new__(cls)
		print ('my name is __new__: %s' % cls)
		return cls._instance


class My_class(Cls_for_new):
	a = '123'

	
instance1 = My_class()
instance2 = My_class()
instance3 = Cls_for_new()

instance1.a = '1234'

print id(instance1)
print id(instance2)
print id(instance3)
print instance1._instance
